#include "lrcpage.h"
#include "ui_lrcpage.h"
#include <QPropertyAnimation>
#include <QDebug>
#include <QFile>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    ui->hidBtn->setIcon(QIcon(":/image/xiala.png"));

    //初始化上移动画
    lrcPageAnimal = new QPropertyAnimation(this,"geometry",this);
    lrcPageAnimal->setDuration(250);
    lrcPageAnimal->setStartValue(QRect(10,10,width(),height()));
    lrcPageAnimal->setEndValue(QRect(10,10 + height(),width(),height()));

    //下拉按钮关联事件
    connect(ui->hidBtn,&QPushButton::clicked,this,[=]{
        qDebug() << "开始";
        lrcPageAnimal->start();
    });

    //当动画结束隐藏按钮
    connect(lrcPageAnimal,&QPropertyAnimation::finished,this,[=]{
        qDebug() << "隐藏";
        this->hide();
    });

    showLrcWordLine(-1);
    
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::praseLrcFile(const QString &lrcFilePath)
{
    this->lrcWordLines.clear();
    //1. 打开文件
    QFile file(lrcFilePath);

    if (!file.open(QFile::ReadOnly))
    {
        qDebug() <<"打开lrc文件: " << lrcFilePath << " ,失败!!!";
        return false;
    }

    qDebug() << "打开文件成功";

    //一次性读取一行歌词
    while (!file.atEnd())
    {
        QString lrcLineWord = file.readLine();

        //解析歌词
        int start = 0,end = 0;
        // 1. 先解析出歌词和时间
        end = lrcLineWord.indexOf(']',start);
        QString lrcTime = lrcLineWord.mid(start,end - start + 1);
        QString lrcWord = lrcLineWord.mid(end + 1).trimmed();
        // 2. 在时间中解析出来分:秒:毫秒
        //截取分
        qint64 lineTime = 0;
        start = 1;
        end = lrcTime.indexOf(":",start);
        lineTime += lrcTime.mid(start,end - start).toInt()* 1000*60;

        //解析秒
        start  = end + 1;
        end = lrcTime.indexOf('.',start);
        lineTime += lrcTime.mid(start,end - start).toInt() * 1000;

        //解析毫秒
        start  = end + 1;
        end = lrcTime.indexOf('.',start);
        if (end == -1)
        {
            lineTime += lrcTime.mid(start).toInt();
        }
        else
        {
            lineTime += lrcTime.mid(start,end - start).toInt();
        }

        // 3. 将改行保存
        this->lrcWordLines.push_back({lineTime,lrcWord});



    }

    for (auto e : lrcWordLines)
    {
        qDebug() << e.lrcTime << ":" << e.lrcText;
    }

    return true;


}

void LrcPage::showLrcWordLine(qint64 time)
{
    // 1. 根据当前所唱歌曲的时间来获取当前所唱歌曲在QVector中的索引
    int index = getLrcWordLineIndex(time);
    // 2. 更新前三行 当前播放 后三行到界面
    if (-1 == index)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲暂无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLrcWordByIndex(index - 3));
        ui->line2->setText(getLrcWordByIndex(index - 2));
        ui->line3->setText(getLrcWordByIndex(index - 1));
        ui->lineCenter->setText(getLrcWordByIndex(index));
        ui->line4->setText(getLrcWordByIndex(index + 1));
        ui->line5->setText(getLrcWordByIndex(index + 2));
        ui->line6->setText(getLrcWordByIndex(index + 3));
    }
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    //将time和QVector中保存的LrcWordLine中的time进行对比
    if (lrcWordLines.isEmpty())
    {
        return -1;
    }

    if (time <= lrcWordLines[0].lrcTime)
    {
        return 0;
    }

    for (int i = 1; i < lrcWordLines.size(); i++)
    {
        if (time > lrcWordLines[i - 1].lrcTime && time < lrcWordLines[i].lrcTime)
        {
            // 第i - 1 行还没有播放完
            return i - 1;
        }
    }

    return lrcWordLines.size() - 1;
}

QString LrcPage::getLrcWordByIndex(int index)
{
    if (index < 0 || index >= lrcWordLines.size())
    {
        return "";
    }

    return lrcWordLines[index].lrcText;
}
